import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: xlr
 * @Date: Created in 下午11:15 2018/11/14
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory() ;
        factory.setHandshakeTimeout( 15000 );
        factory.setHost( "120.79.211.144" );
        factory.setPort( 5672 );
        factory.setVirtualHost( "/" );
        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        String queueName = "test";
        channel.queueDeclare( queueName,true,false,false,null );

        QueueingConsumer consumer = new QueueingConsumer( channel );
        channel.basicConsume( queueName,true,consumer );
        while (true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String msg = new String (delivery.getBody());
            System.out.println(msg);
        }
    }
}
